var Options = 
{
	Domain: {}, 
	SignIn: {}, 
	UI: {}, 
	Blocking: {}, 
};

var storage = 
{
	get : (function(item)
	{
		return localStorage.getItem(item);
	}), 

	set : (function(item, newValue)
	{
		var oldValue = this.get(item);
		localStorage.setItem(item, newValue);
		if(this.useChromeStorage)
		{
			var items = {};
			items[item] = newValue;
			chrome.storage.sync.set(items);
			//NO notification required after sync.set since we handle
			//chrome.storage.onChanged
		}
		else if(this.onChanged && oldValue != newValue)
			this.onChanged(item, newValue, oldValue);
	}), 

	remove : (function(item)
	{
		localStorage.removeItem(item);
		if(this.useChromeStorage)
			chrome.storage.sync.remove(item);
	}), 

	canSync : (function()
	{
		return chrome.storage && chrome.storage.sync && 
			chrome.storage.onChanged;
	}), 

	useChromeStorage : false, 
	init : (function()
	{
		//Initialization is necessary only when chrome.storage is used.
		if(!this.canSync())
			return;
		this.useChromeStorage = true;

		//Handling chrome.storage.onChanged to notify setting changes
		//Note that chrome.storage.onChanged will not fire if a `set` 
		//method is called with the same item value as before, which is
		//consistent with the behavior of storage.onChanged.
		//Note that this event will also fire if settings are changed
		//because of sync. That's why we listen for it.
		chrome.storage.onChanged.addListener(function(changes, namespace)
		{
			if(namespace == "sync")
				for(var item in changes)
				{
					//Set items in localStorage so that new valued can be 
					//read
					localStorage.setItem(item, changes[item].newValue);
					this.onChanged && this.onChanged(item, 
						changes[item].newValue, changes[item].oldValue);
				}
		});
	}), 
};

storage.init();

//=================Properties definition begins===========================
(function()
{

/*
 * Domain:
 *     Force ( boolean ) [true]
 *     AutoDetect ( boolean ) [true]
 *     Current ( string ) ["bbs.stuhome.net"]
 *     Preference ( string ) ["default"] {or "alwaysExternal"}
 *     PreferredInternalDomain ( string ) ["bbs.stuhome.net"]
 *     PreferredExternalDomain ( string ) ["bbs.qshpan.com"]
 */
var storage_Domain_Force = "Domain.Force";
Object.defineProperty(Options.Domain, "Force", 
{
	get: (function(){
		return storage.get(storage_Domain_Force) != "false";
	   	//force single domain by default
	}), 
	set: (function(newValue){
		storage.set(storage_Domain_Force, newValue ? "true" : "false");
	}), 
});

var storage_Domain_AutoDetect = "Domain.AutoDetect";
Object.defineProperty(Options.Domain, "AutoDetect", 
{
	get: (function(){
		return storage.get(storage_Domain_AutoDetect) != "false";
		//auto detect domain by default
	}), 
	set: (function(newValue){
		storage.set(storage_Domain_AutoDetect, newValue ? "true" : "false");
	}), 
});

var storage_Domain_Current = "Domain.Current";
Object.defineProperty(Options.Domain, "Current", 
{
	get: (function(){
		return storage.get(storage_Domain_Current) || 
			"bbs.stuhome.net"; //internal domain by default
	}), 
	set: (function(newValue){
		storage.set(storage_Domain_Current, newValue);
	}), 
});

var storage_Domain_Preference = "Domain.Preference";
Object.defineProperty(Options.Domain, "Preference", 
{
	get: (function(){
		return storage.get(storage_Domain_Preference) || 
			"default"; //default
	}), 
	set: (function(newValue){
		storage.set(storage_Domain_Preference, newValue);
	}), 
});

var storage_Domain_PreferredInternalDomain = "Domain.PreferredInternalDomain";
Object.defineProperty(Options.Domain, "PreferredInternalDomain", 
{
	get: (function(){
		return storage.get(storage_Domain_PreferredInternalDomain) || 
			"bbs.stuhome.net"; //default
	}), 
	set: (function(newValue){
		storage.set(storage_Domain_PreferredInternalDomain, newValue);
	}), 
});

var storage_Domain_PreferredExternalDomain = "Domain.PreferredExternalDomain";
Object.defineProperty(Options.Domain, "PreferredExternalDomain", 
{
	get: (function(){
		return storage.get(storage_Domain_PreferredExternalDomain) || 
			"bbs.qshpan.com"; //default
	}), 
	set: (function(newValue){
		storage.set(storage_Domain_PreferredExternalDomain, newValue);
	}), 
});

/*
 * SignIn:
 *     StaySignedIn ( boolean )
 *     NoWaitAfterSignIn ( boolean )
 */
var storage_SignIn_StaySignedIn = "SignIn.StaySignedIn";
Object.defineProperty(Options.SignIn, "StaySignedIn", 
{
	get: (function(){
		var v = storage.get(storage_SignIn_StaySignedIn);
		return v != "false"; //Enabled by default
	}), 
	set: (function(newValue){
		storage.set(storage_SignIn_StaySignedIn, 
			newValue ? "true" : "false");
	}), 
});

var storage_SignIn_NoWaitAfterSignIn = "SignIn.NoWaitAfterSignIn";
Object.defineProperty(Options.SignIn, "NoWaitAfterSignIn", 
{
	get: (function(){
		var v = storage.get(storage_SignIn_NoWaitAfterSignIn);
		return v != "false"; //Enabled by default
	}), 
	set: (function(newValue){
		storage.set(storage_SignIn_NoWaitAfterSignIn, 
			newValue ? "true" : "false");
	}), 
});

/*
 * UI:
 *     HideHotlist2 ( boolean )
 *     HideAuthorsInHotList ( boolean ) *LABS*
 *     SimpleRead ( boolean )
 */
var storage_UI_HideHotlist2 = "UI.HideHotlist2";
Object.defineProperty(Options.UI, "HideHotlist2", 
{
	get: (function(){
		return storage.get(storage_UI_HideHotlist2) == "true";
		//Disabled by default
	}), 
	set: (function(newValue){
		storage.set(storage_UI_HideHotlist2, 
			newValue ? "true" : "false");
	}), 
});

var storage_UI_HideAuthorsInHotlist = "UI.HideAuthorsInHotlist";
Object.defineProperty(Options.UI, "HideAuthorsInHotlist", 
{
	get: (function(){
		return storage.get(storage_UI_HideAuthorsInHotlist) == "true";
		//Disabled by default
	}), 
	set: (function(newValue){
		storage.set(storage_UI_HideAuthorsInHotlist, 
			newValue ? "true" : "false");
	}), 
});

var storage_UI_SimpleRead = "UI.SimpleRead";
Object.defineProperty(Options.UI, "SimpleRead", 
{
	get: (function(){
		return storage.get(storage_UI_SimpleRead) == "true";
		//Disabled by default
	}), 
	set: (function(newValue){
		storage.set(storage_UI_SimpleRead, 
			newValue ? "true" : "false");
	}), 
});

/*
 * Blocking:
 *     PostsByUsers ( array of string )
 *     SignaturesOfUsers ( array of string )
 *     PhotosOfUsers ( array of string )
 *     ImagesInPostsByUsers ( array of string )
 */
var storage_Blocking_PostsByUsers = "Blocking.PostsByUsers";
Object.defineProperty(Options.Blocking, "PostsByUsers", 
{
	get: (function(){
		var v = storage.get(storage_Blocking_PostsByUsers);
		return v ? JSON.parse(v) : []; //blocking nothing by default
	}), 
	set: (function(newValue){
		if(!newValue || newValue.length == 0)
			storage.remove(storage_Blocking_PostsByUsers);
		else
			storage.set(storage_Blocking_PostsByUsers,  
				JSON.stringify(newValue) );
	}), 
});

var storage_Blocking_SignaturesOfUsers = "Blocking.SignaturesOfUsers";
Object.defineProperty(Options.Blocking, "SignaturesOfUsers", 
{
	get: (function(){
		var v = storage.get(storage_Blocking_SignaturesOfUsers);
		return v ? JSON.parse(v) : []; //blocking nothing by default
	}), 
	set: (function(newValue){
		if(!newValue || newValue.length == 0)
			storage.remove(storage_Blocking_SignaturesOfUsers);
		else
			storage.set(storage_Blocking_SignaturesOfUsers, 
				JSON.stringify(newValue) );
	}), 
});

var storage_Blocking_PhotosOfUsers = "Blocking.PhotosOfUsers";
Object.defineProperty(Options.Blocking, "PhotosOfUsers", 
{
	get: (function(){
		var v = storage.get(storage_Blocking_PhotosOfUsers);
		return v ? JSON.parse(v) : []; //blocking nothing by default
	}), 
	set: (function(newValue){
		if(!newValue || newValue.length == 0)
			storage.remove(storage_Blocking_PhotosOfUsers);
		else
			storage.set(storage_Blocking_PhotosOfUsers, 
				JSON.stringify(newValue) );
	}), 
});

var storage_Blocking_ImagesInPostsByUsers = 
	"Blocking.ImagesInPostsByUsers";
Object.defineProperty(Options.Blocking, "ImagesInPostsByUsers", 
{
	get: (function(){
		var v = storage.get(storage_Blocking_ImagesInPostsByUsers);
		return v ? JSON.parse(v) : []; //blocking nothing by default
	}), 
	set: (function(newValue){
		if(!newValue || newValue.length == 0)
			storage.remove(storage_Blocking_ImagesInPostsByUsers);
		else
			storage.set(storage_Blocking_ImagesInPostsByUsers, 
				JSON.stringify(newValue) );
	}), 
});


//Notifications of setting changes
storage.onChanged = Options.onStorageChanged = (function(item, newValue)
{
	if([
		storage_Domain_Force, 
		storage_Domain_AutoDetect, 
		//storage_Domain_Current, 
		].indexOf(item) != -1)
		Options.Domain.onChanged && Options.Domain.onChanged();
	else if(item == storage_SignIn_StaySignedIn)
		Options.SignIn.onChanged && Options.SignIn.onChanged();
});


})();

